/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */

package l1j.server.server.serverpackets;

import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.utils.IntRange;

// Referenced classes of package l1j.server.server.serverpackets:
// ServerBasePacket

public class S_NewCharPacket extends ServerBasePacket {
	private static final String _S__25_NEWCHARPACK = "[S] New Char Packet";
	private byte[] _byte = null;
	private static final IntRange ValueRange = new IntRange(1, 255);

	public S_NewCharPacket(final L1PcInstance pc) {
		this.buildPacket(pc);
	}

	private void buildPacket(final L1PcInstance pc) {
		this.writeC(S_OPCODE_NEWCHARPACK);
		this.writeS(pc.getName());
		this.writeS("");
		this.writeC(pc.getType());
		this.writeC(pc.get_sex());
		this.writeH(pc.getLawful());
		this.writeH(IntRange.ensure(pc.getMaxHp(), 1, 32767));
		this.writeH(pc.getMaxMp());
		this.writeC(pc.getAc());
		this.writeC(pc.getLevel());
		this.writeC(ValueRange.ensure(pc.getStr()));
		this.writeC(ValueRange.ensure(pc.getDex()));
		this.writeC(ValueRange.ensure(pc.getCon()));
		this.writeC(ValueRange.ensure(pc.getWis()));
		this.writeC(ValueRange.ensure(pc.getCha()));
		this.writeC(ValueRange.ensure(pc.getInt()));
		this.writeC(0);// TODO 3.5C (is Administrator)by99團隊
		// TODO 角色誕生後續的實作尚未補充完畢by99團隊
		// writeD(Integer.parseInt(new
		// SimpleDateFormat("yyyyMMdd").format(pc.getBirthday())));
		// TODO 角色誕生後續的實作尚未補充完畢by99團隊

		// TODO 角色誕生by99團隊
		this.writeD(pc.getSimpleBirthday());
		// TODO 角色誕生by99團隊
		final int checkcode = pc.getLevel() ^ pc.getStr() ^ pc.getDex()
				^ pc.getCon() ^ pc.getWis() ^ pc.getCha() ^ pc.getInt();
		this.writeC(checkcode & 0xFF);
	}

	@Override
	public byte[] getContent() {
		if (this._byte == null) {
			this._byte = this._bao.toByteArray();
		}
		return this._byte;
	}

	@Override
	public String getType() {
		return _S__25_NEWCHARPACK;
	}

}
